package com.weibo4terios.ui.util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;

/**
 * MyTitleFrameLayout.java 2012-10-28 ����3:50:43
 * 
 * @author Wang Tengrui
 */
public class MyTitleFrameLayout extends FrameLayout implements OnGestureListener, OnTouchListener {

	private static final int MAXHEIGHT = 90;
	private int mDistanceY;
	private int startY, finishY;
	View titleView;
	View listView;

	private GestureDetector mGestureDetector;// 手势拦截器

	public MyTitleFrameLayout(Context context) {
		super(context);
		init();
	}

	public MyTitleFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyTitleFrameLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.mGestureDetector = new GestureDetector(this);

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		titleView = getChildAt(1);
		listView = getChildAt(0);
		int titleTop = this.titleView.getTop();
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			this.startY = (int) ev.getY();
		case MotionEvent.ACTION_MOVE:
			this.finishY = (int) ev.getY();
			this.mDistanceY = (this.finishY - this.startY) / 2;// 终点-起点，正数为下拉，负数为上推
			this.startY = this.finishY;
			int offsetDistanceY = titleTop + mDistanceY;// y的位移量

			if (mDistanceY > 0 && titleTop < 0) {// 下拉
				if (offsetDistanceY <= 0) {
					this.titleView.offsetTopAndBottom(mDistanceY);
					this.listView.offsetTopAndBottom(mDistanceY);

				}
				if (offsetDistanceY > 0) {
					this.titleView.offsetTopAndBottom(-titleTop);
					this.listView.offsetTopAndBottom(-titleTop);
					mDistanceY = -titleTop;
				}

			} else if (mDistanceY > 0 && titleTop == 0) {
				mDistanceY = 0;
			}
			if (mDistanceY < 0 && titleTop > -MAXHEIGHT) {// 上推
				if (offsetDistanceY >= -MAXHEIGHT) {
					this.titleView.offsetTopAndBottom(mDistanceY);
					this.listView.offsetTopAndBottom(mDistanceY);
				}
				if (offsetDistanceY < -MAXHEIGHT) {
					this.titleView.offsetTopAndBottom(-MAXHEIGHT - titleTop);
					this.listView.offsetTopAndBottom(-MAXHEIGHT - titleTop);
					mDistanceY = -MAXHEIGHT - titleTop;
				}
			} else if (mDistanceY < 0 && titleTop == -MAXHEIGHT) {
				mDistanceY = -MAXHEIGHT;
			}
			invalidate();
			break;
		}
		super.dispatchTouchEvent(ev);
		return true;
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		View localView1 = getChildAt(1);
		if (this.mDistanceY > 0) {
			this.mDistanceY = 0;
		}
		int t1 = this.mDistanceY;// title顶端与parent的距离
		int r1 = getMeasuredWidth();
		int b1 = MAXHEIGHT + this.mDistanceY;// title底部与parent的距离
		localView1.layout(0, t1, r1, b1);

		View localView2 = getChildAt(0);
		int t2 = MAXHEIGHT + this.mDistanceY;
		int r2 = getMeasuredWidth();
		int b2 = getMeasuredHeight() + MAXHEIGHT + this.mDistanceY;
		localView2.layout(0, t2, r2, b2);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

}
